#include <iostream>
#include <cerrno>
#include <cstring>
#include <string>
#include <assert.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/ipc.h>

using namespace std;

#define PATHNAME "."
#define PROJID 0x666

#define SIZE 1024

int main()
{
    //1.创建key
    int key = ftok(PATHNAME, PROJID);
    if(key == -1)
    {
        cerr << errno << ": " << strerror(errno) << endl;
        exit(-1);
    }

    //2.获取共享内存
    int shmid = shmget(key, SIZE, IPC_CREAT);//共享内存已经被server创建了，这边只需要获取就行
    if(shmid == -1)
    {
        cerr << errno << ": " << strerror(errno) << endl;
        exit(-1);
    }

    //3.建立链接
    char* start = (char*)shmat(shmid, nullptr, 0);

    //4.写入
    char buffer[1024] = {0};
    string str = "i am client my pid is";
    snprintf(buffer, sizeof(buffer), "%s : %d", str.c_str(), getpid());
    strcpy(start, buffer);

    //5.去关联
    int n = shmdt(start);
    assert(n != -1);
    (void)n;

    return 0;
}